package com.hu.auth.common.result;

import lombok.Data;

@Data
public class R<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    // 额外的数据
    private Object extra;

    public R<T> extra(Object data) {
        this.extra = data;
        return this;
    }

    public R() {
    }

    // 返回数据
    protected static <T> R<T> data(T data) {
        R<T> r = new R<>();
        if (data != null) {
            r.setData(data);
        }
        return r;
    }

    public static <T> R<T> data(T body, Integer code, String message) {
        R<T> r = data(body);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    public static <T> R<T> data(T body, RCodeEnum rCodeEnum) {
        R<T> r = data(body);
        r.setCode(rCodeEnum.getCode());
        r.setMessage(rCodeEnum.getMessage());
        return r;
    }

    public static <T> R<T> ok() {
        return R.ok(null);
    }

    public static <T> R<T> ok(T data) {
        return data(data, RCodeEnum.OK);
    }

    public static <T> R<T> fail() {
        return R.fail(null);
    }

    public static <T> R<T> fail(T data) {
        return data(data, RCodeEnum.FAIL);
    }

    public R<T> message(String msg) {
        this.setMessage(msg);
        return this;
    }

    public R<T> code(Integer code) {
        this.setCode(code);
        return this;
    }
}
